package com.barbeque.mall.service.user;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.user.*;
import com.barbeque.mall.bean.user.bo.AddressPageBo;
import com.barbeque.mall.bean.user.bo.FeedbackPageInfo;
import com.barbeque.mall.bean.user.bo.FootPrintPageInfo;
import com.barbeque.mall.bean.user.bo.HistoryPageInfo;
import com.barbeque.mall.bean.wx.feedback.WxMarketFeedback;
import com.barbeque.mall.bean.wx.feedback.WxMarketFeedbackExample;
import com.barbeque.mall.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author why
 * @since 2022/07/16 14:30
 */
@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;

    @Autowired
    WxMarketFeedbackMapper wxMarketFeedbackMapper;


    /**
     * 查看收货地址和查询操作
     *
     * @param info
     * @return
     */
    @Override
    public CommonData<MarketAddress> selectAddress(AddressPageBo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        //排序封装
        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        addressExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        if (info.getName() != null) {
            criteria.andNameLike("%" + info.getName() + "%");
        }
        if (info.getUserId() != null) {
            criteria.andIdEqualTo(info.getUserId());
        }
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(addressExample);
        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(marketAddresses);

        return CommonData.data(marketAddressPageInfo);
    }

    /**
     * 查看会员收藏和查询操作
     *
     * @param userId
     * @param valueId
     * @param info
     * @return
     */
    @Override
    public CommonData<MarketCollect> selectCollect(Integer userId, Integer valueId, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        //排序封装
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }

        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);
        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(marketCollects);

        return CommonData.data(marketCollectPageInfo);
    }

    /**
     * 查看足迹
     *
     * @param info
     * @return
     */
    @Override
    public CommonData<MarketFootprint> selectFootprint(FootPrintPageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        //排序封装
        MarketFootprintExample example = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        //判断条件，查询
        if (info.getUserId() != null) {
            criteria.andUserIdEqualTo(info.getUserId());
        }
        if (info.getGoodsId() != null) {
            criteria.andGoodsIdEqualTo(info.getGoodsId());
        }

        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(example);
        PageInfo<MarketFootprint> marketFootprintPageInfo = new PageInfo<>(marketFootprints);
        return CommonData.data(marketFootprintPageInfo);
    }

    /**
     * 查看搜索历史
     *
     * @param info
     * @return
     */
    @Override
    public CommonData<MarketSearchHistory> selectHistory(HistoryPageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        //排序封装
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        //判断条件，查询
        if (info.getUserId() != null) {
            criteria.andUserIdEqualTo(info.getUserId());
        }
        if (info.getKeyword() != null) {
            criteria.andKeywordLike("%" + info.getKeyword() + "%");
        }

        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(example);
        PageInfo<MarketSearchHistory> marketSearchHistoryPageInfo = new PageInfo<>(marketSearchHistories);


        return CommonData.data(marketSearchHistoryPageInfo);
    }

    /**
     * 查看意见反馈
     *
     * @param info
     * @return
     */
    @Override
    public CommonData<WxMarketFeedback> selectFeedback(FeedbackPageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        //排序封装
        WxMarketFeedbackExample example = new WxMarketFeedbackExample();
        WxMarketFeedbackExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        //判断条件，查询
        if (info.getId() != null) {
            criteria.andUserIdEqualTo(info.getId());
        }
        if (info.getUsername() != null) {
            criteria.andUsernameLike("%" + info.getUsername() + "%");
        }

        List<WxMarketFeedback> wxMarketFeedbacks = wxMarketFeedbackMapper.selectByExample(example);
        PageInfo<WxMarketFeedback> marketFeedbackPageInfo = new PageInfo<>(wxMarketFeedbacks);


        return CommonData.data(marketFeedbackPageInfo);

    }

}
